import { ObjectId } from 'mongodb';
import BaseModel from './MongoBaseModel';

interface Ipassword {
  _id?: string;
  id?: ObjectId;
  name?: string;
  password?: string;
  [propName: string]: any;
}

class Model extends BaseModel<Ipassword> {
  constructor() {
    super('password');
  }

  async findPerson(query) {
    const db = await this.init();
    const res = db
      .aggregate([
        {
          $match: query,
        },
        {
          $lookup: {
            from: 'persons',
            localField: 'personId',
            foreignField: '_id',
            as: 'personInfo',
          },
        },
        {
          $project: {
            _id: 1,
            personId: 1,
            name: 1,
            token: 1,
            role: '$personInfo.role',
          },
        },
        {
          $unwind: '$role',
        },
      ])
      .toArray();
    return res;
  }
}

export default new Model();
